package com.ysstech.activiti.controller;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ysstech.activiti.service.ActivitiRepositoryService;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 流程仓库Service，用于管理流程仓库，例如：部署，删除，读取流程资源
 */
@Slf4j
@Controller
@Api(tags = "流程仓库管理api")
@RequestMapping(value = "/activitiRepository")
public class ActivitiRepositoryController extends BaseController {

    @Autowired
    private ActivitiRepositoryService activitiRepositoryService;

    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "activiti/repository";
    }

    /**
     * 发布流程
     *
     * @return
     */
    @RequestMapping("/repository")
    @ResponseBody
    public RestResult newdeploy( MultipartFile file, String filename) throws Exception {
        activitiRepositoryService.repositoryFlow(file, filename);
        return ResultGenerator.getSuccessResult("发布流程成功");
    }

    /**
     * 查询部署对象信息 对应表（act_re_deployment）
     *
     * @return
     */
    @RequestMapping(value = "/queryDeployment")
    @ResponseBody
    public RestResult queryDeployment(String page, String rows) throws Exception {
        //1:查询部署对象信息，对应表（act_re_deployment）
        Map<String, Object> map = activitiRepositoryService.queryDeployment(this.getPage(page, rows));
        return ResultGenerator.getSuccessResult("查询部署对象信息成功", map);
    }

    /**
     * 删除部署信息
     */
    @RequestMapping(value = "/delDeployment")
    @ResponseBody
    public RestResult delete(String deploymentId) throws Exception {

        //1：获取部署对象ID
        //2：使用部署对象ID，删除流程定义
        if (StringUtils.isNotBlank(deploymentId)) {
            activitiRepositoryService.deleteProcess(deploymentId);
        }
        return ResultGenerator.getSuccessResult("删除流程定义成功");
    }

    /**
     * 点击查看流程图
     */
    @RequestMapping(value = "/viewImage")
    public String viewImage(HttpServletRequest request, String deploymentId, String imageName) {
        request.setAttribute("deploymentId", deploymentId);
        request.setAttribute("imageName", imageName);
        return "activiti/image";
    }

    /**
     * 流程图
     *
     * @throws Exception
     */
    @RequestMapping(value = "/view")
    public String view(String deploymentId, String imageName, HttpServletResponse response) throws Exception {
        //1：获取页面传递的部署对象ID和资源图片名称
        //2：获取资源文件表（act_ge_bytearray）中资源图片输入流InputStream
        InputStream in = activitiRepositoryService.imageInputStream(deploymentId, imageName);
        //3：从response对象获取输出流
        OutputStream out = response.getOutputStream();
        //4：将输入流中的数据读取出来，写到输出流中
        for (int b = -1; (b = in.read()) != -1; ) {
            out.write(b);
        }
        out.close();
        in.close();
        //将图写到页面上，用输出流写
        return null;
    }

}
